package com.boang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boang.entity.Room;
import com.boang.mapper.RoomMapper;
import com.boang.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 22567
 * @description 针对表【room】的数据库操作Service实现
 * @createDate 2025-03-03 19:50:56
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    public Map<Long, Long> countHousesByOwnerIds(List<Long> ownerIds) {
        if (ownerIds == null || ownerIds.isEmpty()) {
            return Collections.emptyMap();
        }
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("owner_id, COUNT(*) as count");
        queryWrapper.in("owner_id", ownerIds);
        queryWrapper.groupBy("owner_id");
        List<Map<String, Object>> maps = roomMapper.selectMaps(queryWrapper);
        return maps.stream().collect(Collectors.toMap(map -> (Long) map.get("owner_id"), map -> (Long) map.get("count")));


    }

}





